package cn.itcast.service.cargo;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service(timeout = 100000)
public class ContractProductServiceImpl implements ContractProductService{
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtCproductDao extCproductDao;


    @Override
    public PageInfo<ContractProduct> findByPage(
            ContractProductExample ContractProductExample, int pageNum, int pageSize) {
        //之后的每一条语句，开始分页查询
        PageHelper.startPage(pageNum,pageSize);
         List<ContractProduct> list = contractProductDao.selectByExample(ContractProductExample);
         PageInfo<ContractProduct> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<ContractProduct> findAll(ContractProductExample ContractProductExample) {
        return contractProductDao.selectByExample(ContractProductExample);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(ContractProduct contractProduct) {
        //因为是添加，所以需要添加id
        contractProduct.setId(UUID.randomUUID().toString());
        //1.计算货物金额

        Double amount = 0d;
        if(contractProduct.getPrice()!=null && contractProduct.getCnumber()!=null){
            amount = contractProduct.getPrice()*contractProduct.getCnumber();
            contractProduct.setAmount(amount);
        }

        //2.修改购销合同
        //添加了货物，就会让购销合同的金额产生变化
        //货物单中含有购销合同的id
         Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //合同货物原本的总金额+新的货物金额
        contract.setTotalAmount(contract.getTotalAmount() + amount);
        //货物种类数量 + 1
        contract.setProNum(contract.getProNum()+ 1);
        //更新合同
        contractDao.updateByPrimaryKeySelective(contract);

        //3.保存货物
        contractProductDao.insertSelective(contractProduct);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        //要修改货物金额
        //这是前端传来的contractProduct对象的总金额，还没有更新到数据库
        //1. 计算修改后的货物金额
        Double amount = 0d;
        if (contractProduct.getPrice()!=null && contractProduct.getCnumber()!=null) {
            amount = contractProduct.getPrice() * contractProduct.getCnumber();
            contractProduct.setAmount(amount);
        }

        //2. 根据货物id查询，获取修改前的货物金额
        ContractProduct cp = contractProductDao.selectByPrimaryKey(contractProduct.getId());
        Double oldAmount = cp.getAmount();

        //3. 修改购销合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //3.1 总金额 = 总金额 + 货物金额
        contract.setTotalAmount(contract.getTotalAmount() + amount - oldAmount);
        //3.2 修改购销合同
        contractDao.updateByPrimaryKeySelective(contract);

        //4. 修改货物
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
    }

    @Override
    public void delete(String id) {
        //A. 根据货物id查询货物金额
         ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
         Double proAmount = contractProduct.getAmount();


        //B. 根据货物id查询附件、累计附件金额
         ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractIdEqualTo(id);
        //查询附件
         List<ExtCproduct> extCproductList = extCproductDao.selectByExample(extCproductExample);

         Double extAmount = 0d;
         //遍历附件集合
        for (ExtCproduct extCproduct : extCproductList) {
            extAmount+= extCproduct.getAmount();
           //因为是删除货物，在这里既然已经查出附件，可以在这里顺带删除
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());
        }

        //C. 修改购销合同
        //在
         Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //C1.总金额 = 总金额 - 货物金额 - 附件金额
        contract.setTotalAmount(contract.getTotalAmount()-proAmount-extAmount);
        //C2.货物数量 = 货物数量 - 1
        contract.setProNum(contract.getProNum()-1);
        //C3.附件数量 = 附件数量 - 查询的附件个数
        contract.setExtNum(contract.getExtNum()-extCproductList.size());
        //C4.修改购销合同
        contractDao.updateByPrimaryKeySelective(contract);
        //D. 删除货物
        contractProductDao.deleteByPrimaryKey(id);
    }
}
